package tools;


import Entity.messageBean;
import control.goodsControl;
import control.messageControl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

@WebServlet("/readMessageServlet")
public class readMessageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String message_id = request.getParameter("message_id");
        String name = request.getParameter("name");
        HttpSession session=request.getSession();
        session.removeAttribute("messagelist");
        messageControl.get_message(request,response);
        if(name==null) {
            //System.out.println("获取当前留言的信息");
            try {
                System.out.println(message_id);
                ArrayList messagelist = null;
                if ((ArrayList) session.getAttribute("messagelist") != null) {
                    messagelist = (ArrayList) session.getAttribute("messagelist");
                    System.out.println(messagelist);
                    messageBean ff = new messageBean();
                    Iterator iter = messagelist.iterator();
                    while (iter.hasNext()) {
                        ff = (messageBean) iter.next();
                        if (ff.getMessage_id().equals(message_id) ){
                            session.setAttribute("messageBean", ff);
                            break;
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            response.sendRedirect("http://localhost:8080/message_content.jsp");
        }
        else
        {
            name = new String(name.getBytes("ISO-8859-1"),"UTF-8");
            System.out.println("检索商品"+name);

            ArrayList messagelist = (ArrayList) session.getAttribute("messagelist");
            System.out.println(messagelist);
            //http://localhost:8080/goodmanageServlet
            ArrayList messagelist2 = new ArrayList();
            messageBean ff = new messageBean();
            Iterator iter = messagelist.iterator();
            while (iter.hasNext()) {
                ff = (messageBean) iter.next();
                if (ff.getMessage_id().contains(name) || ff.getWhat().contains(name) || ff.getOrder_id().contains(name)|| ff.getUse_id().contains(name)) {
                    messagelist2.add(ff);
                }
            }
            session.removeAttribute("messagelist");
            session.setAttribute("messagelist",messagelist2);
            response.sendRedirect("http://localhost:8080/message_manage.jsp");
        }
        return;
    }
}
